package xcmg.device.service.warehouse;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import xcmg.device.dao.entity.warehouse.MaterialTransferDO;
import xcmg.device.dao.entity.warehouse.MaterialTransferDetailDO;
import xcmg.device.dao.mapper.warehouse.MaterialTransferDetailMapper;
import xcmg.device.dao.mapper.warehouse.MaterialTransferMapper;
import xcmg.device.infra.ErrorCode;
import xcmg.device.infra.enums.BillDesc;
import xcmg.device.service.GenDocumentService;
import xcmg.device.service.vo.warehouse.MaterialTransferAddVO;
import xcmg.device.service.vo.warehouse.MaterialTransferDetailVO;
import xcmg.device.service.vo.warehouse.MaterialTransferVO;
import yb.ecp.fast.dto.UserCacheDTO;
import yb.ecp.fast.feign.AuthClient;
import yb.ecp.fast.feign.FastGenClient;
import yb.ecp.fast.infra.infra.ActionResult;
import yb.ecp.fast.infra.util.ListUtil;

import java.util.Date;

@Service
public class MaterialTransferAsyncService {

    @Autowired
    private MaterialTransferMapper materialTransferMapper;

    @Autowired
    private MaterialTransferDetailMapper materialTransferDetailMapper;

    @Autowired
    private FastGenClient fastGenClient;

    @Autowired
    private GenDocumentService genDocumentService;

    @Autowired
    private AuthClient authClient;

    @Async
    public ErrorCode addAsync(MaterialTransferAddVO transferAddVO, String userId, String orgId)throws Exception{
        if(transferAddVO == null || ListUtil.isNullOrEmpty(transferAddVO.getDetailList())){
            return ErrorCode.IllegalArument;
        }
        ActionResult<String> textResult = fastGenClient.textGuid();
        if (textResult.getCode() != ErrorCode.Success.getCode()){
            return ErrorCode.IllegalArument;
        }
        UserCacheDTO userInfo = authClient.getUserDetail(userId).getValue();
        if(userInfo == null){
            return ErrorCode.IllegalArument;
        }
        MaterialTransferVO transferVO = transferAddVO.getHeaderInfo();
        MaterialTransferDO transferDO = new MaterialTransferDO();
        BeanUtils.copyProperties(transferVO, transferDO);
        transferDO.setId(textResult.getValue());
        transferDO.setTransferOrder(genDocumentService.getDocumentNo(BillDesc.MaterialTransfer));
        transferDO.setDeptId(userInfo.getDeptId());
        transferDO.setDeptName(userInfo.getDeptName());
        transferDO.setCreateId(userId);
        transferDO.setCreateName(userInfo.getName());
        transferDO.setCreateDate(new Date());
        transferDO.setOrgId(orgId);

        boolean success = materialTransferMapper.insert(transferDO)>0;
        if(success){
            for(MaterialTransferDetailVO detailVO : transferAddVO.getDetailList()){
                MaterialTransferDetailDO detailDO = new MaterialTransferDetailDO();
                BeanUtils.copyProperties(detailVO, detailDO);
                detailDO.setId(fastGenClient.textGuid().getValue());
                detailDO.setTransferOrder(transferDO.getTransferOrder());
                detailDO.setOrgId(orgId);
                detailDO.setCreateId(transferDO.getCreateId());
                detailDO.setCreateName(transferDO.getCreateName());
                detailDO.setCreateDate(transferDO.getCreateDate());
                success = materialTransferDetailMapper.insert(detailDO)>0;
                if(!success){
                    throw new Exception("生成转库单详情记录失败");
                }
            }
        }
        return success?ErrorCode.Success:ErrorCode.Failure;
    }

}
